#include <windows.h>

#include "displaydialog.h"

HANDLE g_hd;

/* Function to update the text field of the dialog box */
/* Adding the correct device and driver data will work like 
   this update_text() function below */
static void update_text()
{
    HANDLE htext = GetDlgItem(g_hd, IDC_MYTEXT);
    SendMessage(htext, WM_SETTEXT, 0, (LPARAM)"This is the new text");
}

BOOL CALLBACK DisplayDialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_INITDIALOG:
        return TRUE;
    case WM_DESTROY:
        PostQuitMessage(0);
        return TRUE;
    case WM_CLOSE:
        DestroyWindow(hwnd);
        return TRUE;
    case WM_COMMAND:
	switch(LOWORD(wParam))
	{
            /* Pressing IDC_MYBUTTON will update the text */
	    case IDC_MYBUTTON:
                update_text();
	        break;
	}
    }
    return FALSE;
}

/* This creates another window to display the dialog text.
   This will be changed as we want our dialog text to be
   displayed inside of the Display tab of DxDiag,
   not on its own window
*/
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hPrevInst, char *cmdline, int cmdShow)
{
    MSG  msg;
    int status;

    g_hd = CreateDialog(hinst, MAKEINTRESOURCE(DISPLAYTAB_DLG), NULL, (DLGPROC)DisplayDialogProc); 
    if (!g_hd)
    {
        char buf[128];
        wsprintf(buf, "Error x%x", GetLastError ());
        MessageBox(0, buf, "CreateDialog", MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }
    ShowWindow(g_hd, SW_SHOW);

    while ((status = GetMessage(&msg, 0, 0, 0)) != 0)
    {
        if (status == -1)
            return -1;
        if (!IsDialogMessage(g_hd, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return msg.wParam;
}
